package cn.bytecollege.common.controller;

import cn.bytecollege.common.constant.Resource;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * 图片显示控制层
 */
@WebServlet(name = "ShowController", value = "/common/show")
public class ShowController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name = request.getParameter("name");
        File file = new File(Resource.UPLOAD_PATH+name);
        FileInputStream fis = new FileInputStream(file);
        byte[] bytes = new byte[fis.available()];
        ServletOutputStream sos =  response.getOutputStream();
        int k = 0;
        while((k=fis.read(bytes))>-1){
            sos.write(bytes);
        }
        sos.close();
        fis.close();
    }
}
